define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/index',
                    multi_url: 'shop/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'shop',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'add_time',
                search: false,
                pageSize: 10,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('商户名称'), operate: false},
                        {field: 'industry', title: __('所属行业')},
                        {field: 'imgs', title: __('营业执照'), operate: false, events: Table.api.events.images, formatter: Table.api.formatter.images},
                        {field: 'balance', title: __('账号余额'), operate: false},
                        {field: 'user_id', title: __('用户信息'),formatter:function (value, row, index) {
                                if(row.user_id){
                                    return '【'+row.user_id+'】'+row.nickname;
                                }else{
                                    return '';
                                }
                        }, operate: false},
                        {field: 'address', title: __('所在地'), operate: false},
                        {field: 'remark', title: __('拒绝原因'), operate: false},
                        {field: 'status', title: __('审核状态'), formatter: Table.api.formatter.status, searchList: {'0': '待审核', '1': '已完成','-1':'已拒绝','-2':'已冻结'},width: 100},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'status',
                                    title: '通过审核',
                                    text: '通过审核',
                                    confirm: '是否通过审核？通过后该用户即可享受商户等操作权限',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'shop/update_status?status=1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status==0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'click',
                                    title: __('拒绝审核'),
                                    text: '拒绝审核',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    click: function (e, row) {
                                        Layer.prompt({
                                            title: "拒绝原因",
                                            success: function (layero) {
                                                $("input", layero).prop("placeholder", "请输入拒绝审核原因");
                                            }
                                        }, function (value) {
                                            Fast.api.ajax({
                                                url: 'shop/update_status',
                                                data: {remark: value,status:-1,id:row.id},
                                            }, function (data, ret) {
                                                table.bootstrapTable('refresh');
                                                Layer.closeAll();
                                                //$(".btn-refresh").trigger("click");
                                                //return false;
                                            });
                                        });
                                        return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status==0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    
                                },
                                {
                                    name: 'status',
                                    title: '冻结',
                                    text: '冻结',
                                    confirm: '是否确认冻结该商户？',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'shop/update_status?status=-2&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.status==1){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'status',
                                    title: '解冻',
                                    text: '解冻',
                                    confirm: '是否确认解冻该商户？',
                                    classname: 'btn btn-primary btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'shop/update_status?status=1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.status==-2){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ],
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $('.commonsearch-table').removeClass('hidden');

        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
